// <copyright file="ObjectIdentifier.cs" company="TenneySoftware">
// Copyright (c) 2009 All Right Reserved
// </copyright>
// <author>Robert Tenney</author>
// <email>rob10e@yahoo.com</email>
// <date>April 2009</date>
// <summary>Object identifier will assign each and every object a unique identifier as well as track all of them.</summary>

// License:

// Product: RPGRoguelikeEngine .Net
// Author: Robert N. Tenney
// Copyright: June 2009

// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation version 2
// of the License.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

// This license covers all source code, binaries, and other materials related to this project
// unless specifically stated otherwise.

using System;
using System.Collections.Generic;

namespace TenneySoftware.Engine {
    /// <summary>
    /// Object identifier will assign each and every object a unique identifier as well as track all of them.
    /// </summary>
    public class ObjectIdentifier {
        /// <summary>
        /// Random number generator.
        /// </summary>
        private static Random rand;
        
        /// <summary>
        /// The list of code that have already been assigned.
        /// </summary>
        private static List<int> codes;
        
        /// <summary>
        /// The collection of all the object identifiers.
        /// </summary>
        private static ObjectIndentifierCollection identificationMasterList;

        /// <summary>
        /// The description of the object being identified.
        /// </summary>
        private string identifierDescription;
        
        /// <summary>
        /// The unique identification code.
        /// </summary>
        private string identifierCode;
        
        /// <summary>
        /// Initializes a new instance of the ObjectIdentifier class.
        /// </summary>
        public ObjectIdentifier() {
        }

        /// <summary>
        /// Gets the Identification Master List for the engine.
        /// </summary>
        /// <value>
        /// The Identification Master list.</value>
        public static ObjectIndentifierCollection IdentificationMasterList {
            get {
                if (identificationMasterList == null) {
                    identificationMasterList = new ObjectIndentifierCollection();
                }
                
                return identificationMasterList;
            }
        }

        /// <summary>
        /// Gets or sets the identification string.
        /// </summary>
        /// <value>
        /// The identification string.
        /// </value>
        public string IdentifierDescription {
            get {
                return this.identifierDescription;
            }
            
            set {
                this.identifierDescription = value;
            }
        }

        /// <summary>
        /// Gets or sets the identifier code.
        /// </summary>
        /// <value>
        /// The identifier code.
        /// </value>
        public string IdentifierCode {
            get {
                return this.identifierCode;
            }
            
            set {
                this.identifierCode = value;
            }
        }
        
        /// <summary>
        /// Method to generate a random identification number for the object.
        /// </summary>
        /// <returns>The identification number.</returns>
        public static int Generate() {
            if (rand == null) {
                rand = new Random();
            }
            
            if (codes == null) {
                codes = new List<int>();
            }
            
            int temp;
            bool done = false;
            do {
                temp = rand.Next(100000, 999999);
                if (!codes.Contains(temp)) {
                    done = true;
                }
            } while (!done);
            codes.Add(temp);
            return temp;
        }
    }
}
